package com.joshondesign.sdljava.demo;

import com.joshondesign.sdljava.SDL;
import com.joshondesign.sdljava.SDLUtil;
import com.joshondesign.sdljava.SDL_Surface;
import com.joshondesign.sdljava.gfx.*;

import javax.sound.sampled.SourceDataLine;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: josh
 * Date: 9/14/11
 * Time: 2:45 AM
 * To change this template use File | Settings | File Templates.
 */
public class ImageScaleTest implements SDLUtil.InitCallback {
    public static void main(String ... args) {
        SDLUtil.init(new ImageScaleTest());
    }

    SBuffer buffer1, buffer2 = null;
    int counter;
    public void callback() throws Exception {
        Util.standard_init();
        SDL_Surface surface = Util.createResizableWindow(1024, 768);
        final SImage image = new SImage(new File("src/resources/andromeda.jpg"));

        Node node = new Node() {
            @Override
            public void draw(GFX gfx) {
                if(buffer1 == null) {
                    buffer1 = gfx.createBuffer(1024,768);
                    buffer2 = gfx.createBuffer(1024,768);
                }


                //buffer1 to buffer2
                SDL.scaleCopy(
                        (SDL_Surface) (buffer1.getNative()),
                        (SDL_Surface) (buffer2.getNative()),
                        0, 0, buffer1.getWidth(), buffer1.getHeight(),
                        -10, -10, buffer1.getWidth() + 20, buffer1.getHeight() + 20
                );

                //draw image to buffer2
                GFX g2 = buffer2.createGFX();
                g2.setPaint(new SColor(counter%255,(counter*2)%255,(counter*7)%255));
                g2.fillRect((counter*3)%1024,300,200,200);
                g2.dispose();
                /*
                drawScaledImage(
                        (SDL_Surface)image.getNative(),
                        (SDL_Surface)buffer2.getNative(),
                        0,0,image.getWidth(),image.getHeight(),
                        100,100,image.getWidth()+200,image.getHeight()+200
                        );*/
                //draw buffer2 to screen
                gfx.drawBuffer(buffer2, 0, 0);

                //swap buffers
                SBuffer temp = buffer2;
                buffer2 = buffer1;
                buffer1 = temp;

                /*
                drawScaledImage(gfx, image,
                        0,0,image.getWidth(),image.getHeight(),
                        -counter,-counter,
                        image.getWidth()+counter*2, image.getHeight()+counter*2
                        );
                counter++;*/
                counter += 10;
            }
        };

        new Scene(surface)
            .add(new Layer(surface.getFormat()).add(node))
            .setTargetFPS(30)
            .start();
    }

    private void drawScaledImage(SDL_Surface src, SDL_Surface dst,
                                 int sx, int sy, int sw, int sh,
                                 int dx, int dy, int dw, int dh) {
        for(int x=0; x<dw; x++) {
            for(int y=0; y<dh; y++) {
                int ix = (int) (((double)x/(double)dw)*sw);
                int iy = (int) (((double)y/(double)dh)*sh);
                long value = SDL.getPixel(src, ix+sx, iy+sy);

                int fx = x + dx;
                int fy = y + dy;
                if(fx >= 0 && fy >= 0 && fx < dst.getW() && fy < dst.getH()) {
                    SDL.setPixel(dst, fx, fy, value);
                }
            }
        }
    }
}
